﻿using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
//using System.Drawing;
using System.IO;
using System.Web.Caching;
using System.Globalization;


namespace ProjectBase.Utils
{
    public class Utils
    {
        #region Create Instance
        private static Utils instance = null;
        public static Utils Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Utils();
                }
                return instance;
            }
        }
        #endregion

        //public int[] GetSizeImage(int imgWidth, int imgHeight, string sPathUpload, string sPathImage){
        //    int iImgW = 160;
        //    int iImgH = 120;
        //    int[] Parameters = new int[2];
        //    try
        //    {
        //        Image img = System.Drawing.Image.FromFile(sPathUpload + sPathImage);
        //        iImgW = img.Width;
        //        iImgH = img.Height;
        //        float tyle1 = (float)iImgH / iImgW;
        //        float tyle2 = (float)iImgW / iImgH;
        //        if ((iImgW >= iImgH) && (iImgW > imgWidth))
        //        {
        //            iImgW = imgWidth;
        //            iImgH = (int)(tyle1 * iImgW);
        //        }
        //        if ((iImgW < iImgH) && (iImgH > imgHeight))
        //        {
        //            iImgH = imgHeight;
        //            iImgW = (int)(tyle2 * iImgH);
        //        }
        //        Parameters[0] = iImgW;
        //        Parameters[1] = iImgH;
        //        img.Dispose();
        //    }
        //    catch { }
        //    return Parameters;
        //}

        public ArrayList ConvertDT(DataTable dt)
        {
            ArrayList converted = new ArrayList(dt.Rows.Count);
            foreach (DataRow row in dt.Rows)
            {
                converted.Add(row);
            }
            return converted;
        }

        public static Cache Cache
        {
            get
            {
                HttpContext ctx = HttpContext.Current;
                if (null == ctx)
                    return HttpRuntime.Cache;
                else
                    return ctx.Cache;
            }
        }

        public static String FitlerBadString(String fitlerString)
        {
            fitlerString = fitlerString.Replace("'", "");
            fitlerString = fitlerString.Replace("\"", "");
            fitlerString = fitlerString.Replace(@"\", "");
            fitlerString = fitlerString.Replace("<", "&lt;");
            fitlerString = fitlerString.Replace(">", "&gt;");
            return fitlerString;
        }
        /// <summary>
        /// Chuyển tiếng việt có dấu thành không dấu (Rewrite...)
        /// </summary>
        /// <param name="unicodeString"></param>
        /// <returns></returns>
        public static String VietNamese2English(string unicodeString)
        {
            try
            {
                //Remove VietNamese character
                unicodeString = unicodeString.ToLower();
                unicodeString = Regex.Replace(unicodeString, "[áàảãạâấầẩẫậăắằẳẵặ]", "a");
                unicodeString = Regex.Replace(unicodeString, "[ĂÂÀẢÃẠÁẰẲẴẶẮẦẨẪẬẤ]", "A");
                unicodeString = Regex.Replace(unicodeString, "[éèẻẽẹêếềểễệ]", "e");
                unicodeString = Regex.Replace(unicodeString, "[ÈẺẼẸÉỀỂỄỆẾÊ]", "E");
                unicodeString = Regex.Replace(unicodeString, "[iíìỉĩị]", "i");
                unicodeString = Regex.Replace(unicodeString, "[IÌỈĨỊÍ]", "I");
                unicodeString = Regex.Replace(unicodeString, "[óòỏõọơớờởỡợôốồổỗộ]", "o");
                unicodeString = Regex.Replace(unicodeString, "[ÔƠÒỎÕỌÓỒỔỖỘỐỜỞỠỢỚ]", "O");
                unicodeString = Regex.Replace(unicodeString, "[úùủũụưứừửữự]", "u");
                unicodeString = Regex.Replace(unicodeString, "[ƯÙỦŨỤÚỪỬỮỰỨ]", "U");
                unicodeString = Regex.Replace(unicodeString, "[yýỳỷỹỵ]", "y");
                unicodeString = Regex.Replace(unicodeString, "[YỲỶỸỴÝ]", "Y");
                unicodeString = Regex.Replace(unicodeString, "[đ]", "d");
                unicodeString = Regex.Replace(unicodeString, "[Đ]", "D");
                //NCHAR(272)+ NCHAR(208)

                //Remove special character
                unicodeString = Regex.Replace(unicodeString, "\"", "");
                unicodeString = Regex.Replace(unicodeString, "[`~!@#$%^&*()-+=?/>.<,{}[]|]\\:]", "");
                
                //Remove space
                unicodeString = unicodeString.Trim();
                unicodeString = unicodeString.Replace(" ", "-");
                unicodeString = unicodeString.Replace("--", "-");
                unicodeString = unicodeString.Replace(":", "");

                return unicodeString;
            }
            catch (Exception)
            {
                return "";
            }
        }

        private static String FilterNotChar(string unicodeString)
        {
            try
            {
                unicodeString = unicodeString.ToLower();
                string sChar = "[áàảãạâấầẩẫậăắằẳẵặéèẻẽẹêếềểễệiíìỉĩịóòỏõọơớờởỡợôốồổỗộúùủũụưứừửữựyýỳỷỹỵđqwertyuiopasdfghjklzxcvbnm]";
                unicodeString = Regex.Replace(unicodeString, sChar, "");                
                unicodeString = Regex.Replace(unicodeString, "\"", "");
                unicodeString = Regex.Replace(unicodeString, "[`~!@#$%^&*()-+=?/>.<,{}[]|]\\:]", "");
                unicodeString = unicodeString.Trim();                

                return unicodeString;
            }
            catch (Exception)
            {
                return "0";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="val">Tham so chua du lieu goc-chua trong phan du lieu tim kiem</param>
        /// <param name="val1">Tham so chua du lieu dua vao tim kiem</param>
        /// <param name="toantu"></param>
        /// <param name="kieu"></param>
        /// <returns></returns>
        public static bool SoSanhToanTu(string val,string val1,string toantu, string kieu)
        {
            CompareInfo compareStr = CultureInfo.InvariantCulture.CompareInfo;

            switch (toantu)
            {
                case "=":
                    switch (kieu)
                    {
                        case "STRING":
                            int i = compareStr.IndexOf(val, val1, CompareOptions.IgnoreCase);
                            return (i!=-1);
                        case "NUMBER":
                            val = FilterNotChar(val);
                            val1 = FilterNotChar(val1);
                            return (CUtilities.IntegerForNull(val).Equals(CUtilities.IntegerForNull(val1)));
                        case "DATE":
                            return (CUtilities.ConvertDate2Data(val) == CUtilities.ConvertDate2Data(val1));
                    }
                    break;
                case ">":
                    switch (kieu)
                    {
                        case "STRING":
                            return (compareStr.Compare(val, val1, CompareOptions.IgnoreCase) == 1);
                        case "NUMBER":
                            val = FilterNotChar(val);
                            val1 = FilterNotChar(val1);
                            return (CUtilities.IntegerForNull(val) > CUtilities.IntegerForNull(val1));
                        case "DATE":
                            return (CUtilities.ConvertDate2Data(val) > CUtilities.ConvertDate2Data(val1));
                    }
                    break;
                case "=>":
                    switch (kieu)
                    {
                        case "STRING":
                            return (compareStr.Compare(val, val1, CompareOptions.IgnoreCase) != -1);
                        case "NUMBER":
                            val = FilterNotChar(val);
                            val1 = FilterNotChar(val1);
                            return (CUtilities.IntegerForNull(val) >= CUtilities.IntegerForNull(val1));
                        case "DATE":
                            return (CUtilities.ConvertDate2Data(val) >= CUtilities.ConvertDate2Data(val1));
                    }
                    break;
                case "<":
                    switch (kieu)
                    {
                        case "STRING":
                            return (compareStr.Compare(val, val1, CompareOptions.IgnoreCase) == -1);
                        case "NUMBER":
                            val = FilterNotChar(val);
                            val1 = FilterNotChar(val1);
                            return (CUtilities.IntegerForNull(val) < CUtilities.IntegerForNull(val1));
                        case "DATE":
                            return (CUtilities.ConvertDate2Data(val) < CUtilities.ConvertDate2Data(val1));
                    }
                    break;
                case "<=":
                    switch (kieu)
                    {
                        case "STRING":
                            return (compareStr.Compare(val, val1, CompareOptions.IgnoreCase) != 1);
                        case "NUMBER":
                            val1 = FilterNotChar(val1);
                            return (CUtilities.IntegerForNull(val) <= CUtilities.IntegerForNull(val1));
                        case "DATE":
                            return (CUtilities.ConvertDate2Data(val) <= CUtilities.ConvertDate2Data(val1));
                    }

                    break;
                default:
                    return false;
            }
            return false;

        }

        /// <summary>
        /// So sanh 2 gia tri co bang nhau?
        /// </summary>
        /// <param name="val"></param>
        /// <param name="val1"></param>
        /// <returns></returns>
        public static bool SoSanhToanTu(string val, string val1)
        {
            CompareInfo compareStr = CultureInfo.InvariantCulture.CompareInfo;
            switch (GetKieuDuLieu(val1))
            {
                case "STRING":
                    int i = compareStr.IndexOf(val, val1, CompareOptions.IgnoreCase);
                    return (i != -1);
                case "NUMBER":
                    val = FilterNotChar(val);
                    val1 = FilterNotChar(val1);
                    return (CUtilities.IntegerForNull(val).Equals(CUtilities.IntegerForNull(val1)));
                case "DATE":
                    return (CUtilities.ConvertDate2Data(val) == CUtilities.ConvertDate2Data(val1));
                default:
                    break;
            }
            return false;

        }

        /// <summary>
        /// Kiem tra kieu cua du lieu dua vao
        /// </summary>
        /// <param name="val">Chuoi thong tin can kiem tra</param>
        /// <returns></returns>
        private static String GetKieuDuLieu(String val)
        {
            if (CUtilities.IsNumeric(val))
            {
                return "NUMBER";
            }
            else if (CUtilities.IsDate(val))
            {
                return "DATE";
            }else{

                return "STRING";
            }

        }

        /// <summary>
        /// Đếm số lần xuất hiện của một chuổi
        /// </summary>
        /// <param name="matchStr"></param>
        /// <param name="searchedStr"></param>
        /// <returns></returns>
        public static int FindAll(string matchStr, string searchedStr)
        {
            int startPos = 0;
            int foundPos = -1;
            int count = 0;
            do
            {
                foundPos = searchedStr.IndexOf(matchStr, startPos);
                if (foundPos > -1)
                {
                    startPos = foundPos + 1;
                    count++;
                }
            } while (foundPos > -1 && startPos < searchedStr.Length);
            return count;
        }

    }

}
